package cn.iocoder.yudao.module.ems.controller.admin.eqtype;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.eqtype.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.eqtype.EqTypeDO;
import cn.iocoder.yudao.module.ems.convert.eqtype.EqTypeConvert;
import cn.iocoder.yudao.module.ems.service.eqtype.EqTypeService;

@Api(tags = "管理后台 - 设备类别")
@RestController
@RequestMapping("/ems/eq-type")
@Validated
public class EqTypeController {

    @Resource
    private EqTypeService eqTypeService;

    @PostMapping("/create")
    @ApiOperation("创建设备类别")
    @PreAuthorize("@ss.hasPermission('ems:eq-type:create')")
    public CommonResult<Long> createEqType(@Valid @RequestBody EqTypeCreateReqVO createReqVO) {
        return success(eqTypeService.createEqType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新设备类别")
    @PreAuthorize("@ss.hasPermission('ems:eq-type:update')")
    public CommonResult<Boolean> updateEqType(@Valid @RequestBody EqTypeUpdateReqVO updateReqVO) {
        eqTypeService.updateEqType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除设备类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:eq-type:delete')")
    public CommonResult<Boolean> deleteEqType(@RequestParam("id") Long id) {
        eqTypeService.deleteEqType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得设备类别")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:eq-type:query')")
    public CommonResult<EqTypeRespVO> getEqType(@RequestParam("id") Long id) {
        EqTypeDO eqType = eqTypeService.getEqType(id);
        return success(EqTypeConvert.INSTANCE.convert(eqType));
    }

//    @GetMapping("/list")
//    @ApiOperation("获得设备类别列表")
//    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('ems:eq-type:query')")
//    public CommonResult<List<EqTypeRespVO>> getEqTypeList(@RequestParam("ids") Collection<Long> ids) {
//        List<EqTypeDO> list = eqTypeService.getEqTypeList(ids);
//        return success(EqTypeConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/list")
    @ApiOperation("获取设备类别列表")
    @PreAuthorize("@ss.hasPermission('ems:eq-type:query')")
    public CommonResult<List<EqTypeRespVO>> listEqTypes(EqTypeListReqVO reqVO) {
        List<EqTypeDO> list = eqTypeService.getSimpleEqTypes(reqVO);
        list.sort(Comparator.comparing(EqTypeDO::getSort));
        return success(EqTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取设备类别精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<EqTypeSimpleRespVO>> getSimpleEqTypes() {
        // 获得分类列表，只要开启状态的
        EqTypeListReqVO reqVO = new EqTypeListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<EqTypeDO> list = eqTypeService.getSimpleEqTypes(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(EqTypeDO::getSort));
        return success(EqTypeConvert.INSTANCE.convertList02(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得设备类别分页")
    @PreAuthorize("@ss.hasPermission('ems:eq-type:query')")
    public CommonResult<PageResult<EqTypeRespVO>> getEqTypePage(@Valid EqTypePageReqVO pageVO) {
        PageResult<EqTypeDO> pageResult = eqTypeService.getEqTypePage(pageVO);
        return success(EqTypeConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/export-excel")
//    @ApiOperation("导出设备类别 Excel")
//    @PreAuthorize("@ss.hasPermission('ems:eq-type:export')")
//    @OperateLog(type = EXPORT)
//    public void exportEqTypeExcel(@Valid EqTypeExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<EqTypeDO> list = eqTypeService.getEqTypeList(exportReqVO);
//        // 导出 Excel
//        List<EqTypeExcelVO> datas = EqTypeConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "设备类别.xls", "数据", EqTypeExcelVO.class, datas);
//    }

}
